﻿using System;
using System.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using mojoPortal.Web.RS.CRM.AppCode.Lib;
using Microsoft.Reporting.WebForms;
 

namespace mojoPortal.Web.RS.CRM
{
    public partial class RSClientReport : RSBasePage
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                RSUserIdentity userIdentity = GetRSUserIdentity(); // new RSUserIdentity();
                RSCRMDataLookupLoader.LoadAgentDDL(userIdentity.RSCompanyId, ddlAgent, "", "0");
            }
        }
        protected void btnRunReport_Click(object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            int selAgentId = int.Parse(ddlAgent.SelectedValue);
            SetReport(userIdentity.RSCompanyId, selAgentId);
        }
        protected void ObjectDataSource_OnObjectCreated(object sender, ObjectDataSourceEventArgs e)
        {
            if (e.ObjectInstance != null)
            {
                SqlConnection conn = new SqlConnection();
                conn.ConnectionString = ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString();
                e.ObjectInstance.GetType().GetProperty("Connection").SetValue(e.ObjectInstance, conn, null);
            }
        }
        private void SetReport(int companyid, int agentid)
        {
            RSCRMClientDataSource.SelectParameters["companyid"].DefaultValue = companyid.ToString();
            RSCRMClientDataSource.SelectParameters["agentid"].DefaultValue = agentid.ToString();

            LocalReport rep = ReportViewer1.LocalReport;
            rep.ReportPath = "RS\\AppData\\CRM\\Reports\\RDLC\\RSCRMClientReport.rdlc";
            rep.DataSources.Clear();

            ReportDataSource dsClient = new ReportDataSource("tblClient", RSCRMClientDataSource);

            rep.DataSources.Add(dsClient);
            rep.Refresh();

            ReportParameter reportparam;

            string Agent = ddlAgent.SelectedItem.Text;
            if (Agent.Length > 0)
            {
                reportparam = new ReportParameter("ReportHeaderText", Agent + " - Clients");
            }
            else
            {
                reportparam = new ReportParameter("ReportHeaderText", "Clients");
            }
            ReportViewer1.LocalReport.SetParameters(reportparam);
            reportparam = null;

            foreach (ListItem MyItem in chkReportColumns1.Items)
            {
                if (MyItem.Selected == true)
                {
                    reportparam = new ReportParameter(MyItem.Text, "1");
                }
                else
                {
                    reportparam = new ReportParameter(MyItem.Text, "0");
                }
                ReportViewer1.LocalReport.SetParameters(reportparam);
                reportparam = null;
            }

        }
    }

}
